﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using Buccaneer.Types;
using PluginInterface;
using Buccontrols;
using System.IO;

namespace Buccaneer
{
    public partial class MainProgram
    {
        public static PluginServices Plugins;
        public static Thread ConsoleThread;
        public static AvailablePlugin selectedPlugin;
        public static PluginDaddy MainForm;

        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);

            #region Setup Console

            Global.EnableCommandTyping();

            Console.BufferWidth = 200;
            TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
            Debug.Listeners.Add(myWriter);

            #endregion

            #region TODO: Check arguments

            // TODO: load the plugin associated with the filetype of the argument('s)

            #endregion

            Console.WriteLine("Starting Buccaneer...");

            #region 1: Load options

            Options.Initialize();
            Options.LoadOptions();
            
            Options.WriteComment("Loaded Options...");

            #endregion

            #region 2: Load images/layout

            Options.LoadImageDirectory(Options.LayoutFolder);
 
            Options.WriteComment("Loaded Layout...");

            #endregion

            #region 3: Register actions

            MainProgram.Plugins = new PluginServices();
            MainProgram.MainForm = new PluginDaddy(MainProgram.Plugins);
            MainProgram.Plugins.RegisterAction(new ExitAction(MainProgram.MainForm));
            MainProgram.Plugins.RegisterAction(new LoadRescImgAction());
            MainProgram.Plugins.RegisterAction(new ShowRescImgAction());
            MainProgram.Plugins.RegisterAction(new HelpAction());
            MainProgram.Plugins.RegisterAction(new AddPluginFormAction());
            MainProgram.Plugins.RegisterAction(new LoadLibDirectoryAction());
            MainProgram.Plugins.RegisterAction(new LoadLibraryAction());
            MainProgram.Plugins.RegisterAction(new LoadPluginAction());
            MainProgram.Plugins.RegisterAction(new LoadStartupAction());
            MainProgram.Plugins.RegisterAction(new UnloadPluginAction());
            MainProgram.Plugins.RegisterAction(new ShowPluginsAction());
            MainProgram.Plugins.RegisterAction(new ActivatePluginAction());
            MainProgram.Plugins.RegisterAction(new SetCommentTypesAction());
            MainProgram.Plugins.RegisterAction(new AddToCommentTypesAction());
            MainProgram.Plugins.RegisterAction(new RemoveFromCommentTypesAction());
            MainProgram.Plugins.RegisterAction(new AddMenuAction());
            MainProgram.Plugins.RegisterAction(new AddAddressBarAction());
            MainProgram.Plugins.RegisterAction(new ShowEnvironmentVariabeleAction());
            MainProgram.Plugins.RegisterAction(new SaveOptionsAction());
            MainProgram.Plugins.RegisterAction(new OpenFileAction());

            Options.WriteComment("Loaded actions...");

            #endregion

            #region 4: Load Plugins

            Plugins.Act("loadlibdir", new string[]
            {
                Options.PluginsFolder
            });
            Plugins.Act("loadstartup", null);

            Options.WriteComment("Loaded plugins...");

            #endregion

            #region 5: Create form

            Options.WriteComment("Loaded Form...");

            #endregion

            #region 6: Run Application

            Console.WriteLine("Running Application..."); Console.WriteLine();
            Application.Run(MainForm);
            Application.Exit();
            Environment.Exit(0);

            #endregion
        }
    }

    public class ExitAction : PluginAction
    {
        public ExitAction(PluginDaddy pd)
        {
            base.name = "exit";
            base.func = new ActionFunction(exit);
            base.pars.Add("reason", "opt: the reason for the application to exit");
            base.pluginDaddy = pd;
        }

        public void exit(string[] args)
        {
            base.pluginDaddy.Close();
            Application.Exit();
        }
    }

    public class HelpAction : PluginAction
    {
        public HelpAction()
        {
            base.name = "help";
            base.func = new ActionFunction(ShowHelp);
        }

        public void ShowHelp(string[] args)
        {
            if (args == null || args[0] == "")
                MainProgram.Plugins.GetActionHelp();
            else
                MainProgram.Plugins.Act(args[0], null);
        }
    }
}